package cn.stylefeng.guns.modular.goods.company.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goods.company.entity.Company;
import cn.stylefeng.guns.modular.goods.company.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.goods.company.model.params.CompanyParam;
import cn.stylefeng.guns.modular.goods.company.model.result.CompanyResult;
import  cn.stylefeng.guns.modular.goods.company.service.CompanyService;
import cn.stylefeng.guns.wrapper.MyWrapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syt
 * @since 2020-10-12
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Override
    public void add(CompanyParam param){
        Company entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CompanyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CompanyParam param){
        Company oldEntity = getOldEntity(param);
        Company newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CompanyResult findBySpec(CompanyParam param){
        return null;
    }

    @Override
    public List<CompanyResult> findListBySpec(CompanyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CompanyParam param){
        Page pageContext = getPageContext();
//        IPage page = this.baseMapper.customPageList(pageContext, param);
        Page<Map<String, Object>> list = this.baseMapper.customPageMapList(pageContext, param);
        Page<Map<String, Object>> wrap = new MyWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    @Override
    public List<CompanyResult> getALL(CompanyParam param) {
        return this.baseMapper.customList(param);
    }

    private Serializable getKey(CompanyParam param){
        return param.getCompanyId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Company getOldEntity(CompanyParam param) {
        return this.getById(getKey(param));
    }

    private Company getEntity(CompanyParam param) {
        Company entity = new Company();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
